package components.contorls.superClass
{
	import components.core.UIComponent;
	import components.events.FTELinkEvent;
	import components.fte.FTEElement;
	import components.utils.EventListenerUtil;
	
	import flash.display.Sprite;
	import flash.events.TextEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextFieldAutoSize;
	import flash.text.engine.TextBaseline;
	import flash.text.engine.TextBlock;
	import flash.text.engine.TextLine;

	dynamic public class BaseLabel extends UIComponent
	{
		private var _text:String="Label";

		private var _align:String="left";

		private var _color:Number=0x0;

		private var _size:uint=12;

		private var _wordWrap:Boolean=false;

		private var _autoSize:Boolean=false;

		private var _bold:Boolean=false;

		private var _fteElement:FTEElement;

		private var _textBlock:TextBlock;

		private var _textSp:Sprite;

		private var _vGap:Number=2;

		private var _fontName:String="宋体";
		
		private var _hGap:Number=0;
		
		public function BaseLabel()
		{
			super();

//            mouseChildren = mouseEnabled = false;
		}
		
		/***************************************
		 *
		 * 显示对象声明
		 *
		 * **************************************/

		public function get textSp():Sprite
		{
			return _textSp;
		}

		/***************************************
		 *
		 * 验证
		 *
		 * **************************************/
		protected override function commitProperties():void
		{
			super.commitProperties();

			if (!_initialized)
			{
				_fteElement=new FTEElement();
				cleanObjList.push(_fteElement);
				
				EventListenerUtil.addEventListener(_fteElement,FTELinkEvent.LINK_CLICK, linkClickHandler);
				_fteElement.defaultBold=bold;
				_fteElement.defaultFontColor=color;
				_fteElement.defaultFontName=fontName;
				_fteElement.defaultFontSize=size;
					
				_textBlock=new TextBlock();
				_textBlock.baselineZero=TextBaseline.IDEOGRAPHIC_TOP;
					
				_textSp=new Sprite();
				addChild(_textSp);
				
				setTFFilter();
			}
		}

		protected override function measureSizes():Boolean
		{
			var change:Boolean=super.measureSizes();

			return change;
		}

		protected override function updateDisplayList():void
		{
			super.updateDisplayList();

			createText();
		}

		private function createText():void
		{
			cleanLine();

			_textBlock.content=_fteElement.createElement(text);
			createLine();
		}

		private function createLine():void
		{
			var w:Number=width;
			if (autoSize)
				w=1000000;

			var textLine:TextLine=_textBlock.createTextLine(null, w);
			var y:Number=0;

			while (textLine)
			{
				if (autoSize)
				{
					myWidth=textLine.width;
					_textSp.addChild(textLine);
					break;
				}

				//对齐
				if (align == TextFieldAutoSize.RIGHT)
					textLine.x=width - textLine.width;
				if (align == TextFieldAutoSize.CENTER)
					textLine.x=(width - textLine.width) * 0.5;

				textLine.y=y;
				y+=textLine.height + _vGap;

				if (y > height)
					break;

				_textSp.addChild(textLine);
				textLine=_textBlock.createTextLine(textLine, w);

				//换行
				if (!wordWrap)
					break;
			}

			_textBlock.releaseLineCreationData();
		}

		private function cleanLine():void
		{
			while (_textSp.numChildren > 0)
				_textSp.removeChildAt(0);
		}

		/***************************************
		 *
		 * 组件定义
		 *
		 * **************************************/

		[Inspectable(defaultValue="Label")]
		public function get text():String
		{
			return _text;
		}

		public function set text(value:String):void
		{
			if (_text == value)
				return;

			_text=value;
			if (_text == null)
				_text="";

			invalidate();
		}

		[Inspectable(defaultValue="left", type="List", enumeration="left,center,right")]
		public function get align():String
		{
			return _align;
		}

		public function set align(value:String):void
		{
			_align=value;

			invalidate();
		}

		[Inspectable(defaultValue="#000000", type="Color")]
		public function get color():Number
		{
			return _color;
		}

		public function set color(value:Number):void
		{
			_color=value;
			_fteElement.defaultFontColor=value;

			invalidate();
		}

		[Inspectable(defaultValue="12")]
		public function get size():uint
		{
			return _size;
		}

		public function set size(value:uint):void
		{
			_size=value;
			_fteElement.defaultFontSize=value;

			invalidate();
		}


		[Inspectable(defaultValue="false")]
		public function get wordWrap():Boolean
		{
			return _wordWrap;
		}

		public function set wordWrap(value:Boolean):void
		{
			_wordWrap=value;

			invalidate();
		}

		[Inspectable(defaultValue="false")]
		public function get autoSize():Boolean
		{
			return _autoSize;
		}

		public function set autoSize(value:Boolean):void
		{
			_autoSize=value;

			invalidate();
		}

		[Inspectable(defaultValue="false")]
		public function get bold():Boolean
		{
			return _bold;
		}

		public function set bold(value:Boolean):void
		{
			_bold=value;
			_fteElement.defaultBold=value;

			invalidate();
		}

		[Inspectable(defaultValue="2")]
		public function get vGap():Number
		{
			return _vGap;
		}

		public function set vGap(value:Number):void
		{
			_vGap=value;

			invalidate();
		}
		
		[Inspectable(defaultValue="0")]
		public function get hGap():Number
		{
			return _hGap;
		}
		
		public function set hGap(value:Number):void
		{
			_hGap=value;
			_fteElement.defaultHGap=value;
			
			invalidate();
		}

		[Inspectable(defaultValue="宋体")]
		public function get fontName():String
		{
			return _fontName;
		}

		public function set fontName(value:String):void
		{
			_fontName=value;
			_fteElement.defaultFontName=value;

			invalidate();
		}

		/***************************************
		 *
		 * 其他代码
		 *
		 * **************************************/

		public override function destroy():void
		{
			super.destroy();
			
			_textBlock=null;
			_fteElement=null;
			_text=null;
			_align=null;
		}

		protected override function setTFFilter():void
		{
			if (tfFilterEnable)
				_textSp.filters=[new GlowFilter(tfFilterColor, 1, 2, 2, 200)];
			else
				_textSp.filters=[];
		}

		protected function linkClickHandler(event:FTELinkEvent):void
		{
			event.stopImmediatePropagation();
			dispatchEvent(new TextEvent(TextEvent.LINK, false, false, event.data));
		}

		public function get textBlock():TextBlock
		{
			return _textBlock;
		}
		
		public function getAtomByPoint(stageX:Number, stageY:Number):int
		{
			var textLine:TextLine=textBlock.firstLine;
			var atomIndex:int=-1;
			while (textLine)
			{
				atomIndex=textLine.getAtomIndexAtPoint(stageX, stageY);
				if (atomIndex == -1)
					textLine=textLine.nextLine;
				else
					break;
			}
			
			if (atomIndex != -1)
			{
				var p:Point=globalToLocal(new Point(stageX, stageY));
				var rect:Rectangle=textLine.getAtomBounds(atomIndex);
				var center:Number=rect.left + (rect.width >> 1);
				
				if (p.x > center)
					return atomIndex + 1;
				else
					return atomIndex;
			}
			else
				return getLastAtom;
		}
		
		public function get getLastAtom():int
		{
			var textLine:TextLine=textBlock.firstLine;
			var atomIndex:int=0;
			while(textLine)
			{
				atomIndex+=textLine.atomCount;
				textLine=textLine.nextLine;
			}
			
			return atomIndex;
		}
		
		public function getCharBounds(index:int):Rectangle
		{
			var rect:Rectangle;
			
			var textLine:TextLine=textBlock.getTextLineAtCharIndex(index);
			if (textLine)
				rect=textLine.getAtomBounds(textLine.getAtomIndexAtCharIndex(index));
			
			return rect;
		}
	}
}